/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.vista.auxiliars;




import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import comandaXM.accions.AuxiliarsAct; 
import comandaXM.classes.manteniment.auxiliar.Pais;

import comandaXM.excepcions.ExceptionHibernate; 
import comandaXM.excepcions.ExceptionXML; 
import comandaXM.utils.JDialogXM;
import comandaXM.utils.TDSLanguageUtils;
import comandaXM.utils.TableModel;


/**
 *
 * @author xmarti
 */
public abstract class JDPaisos extends JDialogXM{    
    private List<Pais>paisos;
    private Pais pais;
    private AuxiliarsAct accio;
    private JPAuxiliarData3 jPData;
    public JDPaisos(){        
        super(null,true,"JdPais",TDSLanguageUtils.getMessage("jDialogPais.JDialog.caption"));        
        this.accio=new AuxiliarsAct (); 
        this.jPData= new JPAuxiliarData3(true) {

            @Override
            public void movePosition(int e) {
                  incrementarJTableXM(e);
            }
        };
        this.jPData.addKeyCodeListener(jPData);
        super.getjPanelData().setViewportView(this.jPData);
        try {
            super.chargeTable();
        } catch (ExceptionHibernate ex) {
            Logger.getLogger(JDPaisos.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExceptionXML ex) {
            Logger.getLogger(JDPaisos.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }

    @Override
    public TableModel getModel(String textToSearch) throws ExceptionHibernate {
       TableModel dadesTaula = null;                
            this.paisos=(List<Pais>) this.accio.getPaisos(textToSearch);
            String [] columns={"",
                          TDSLanguageUtils.getMessage("jDialogPais.JTable.codi.titol"),                               
                          TDSLanguageUtils.getMessage("jDialogPais.JTable.abreviatura.titol"),
                          TDSLanguageUtils.getMessage("jDialogPais.JTable.nom.titol"),                          
                    
                          };    
            Object[][] dades=null;
           
            if (this.paisos.size()>0){
                dades=new Object[this.paisos.size()][columns.length];                          
                Integer i=0;            
                for (Iterator it = this.paisos.iterator(); it.hasNext();) {
                    Pais c = (Pais) it.next();                
                    dades[i][0]= c; 
                    if (c.getCodi() ==(null))dades[i][2]="-"; else dades[i][1]= c.getCodi();                     
                    if (c.getAbreviatura() ==(null))dades[i][2]="-"; else dades[i][2]= c.getAbreviatura(); 
                    if (c.getNom() ==(null))dades[i][3]="-"; else dades[i][3]= c.getNom();                    
                    i++;
                }     
            }                  
        return new TableModel(columns, dades);   
    }

    @Override    
    public void getData()  {                
        this.pais.setCodi(this.jPData.getCodeAuxiliar());
        this.pais.setNom(this.jPData.getNameAuxiliar());
        this.pais.setAbreviatura(this.jPData.getCodeAuxiliar1());        
    }
    
    @Override
    public void setData() {
       if(super.isInsert()) this.pais=new Pais();
       else this.pais=(Pais) super.getObject();
       this.jPData.setNameAuxiliar(this.pais.getNom());                       
       this.jPData.setCodeAuxiliar1(this.pais.getAbreviatura()); 
       this.jPData.setCodeAuxiliar(String.valueOf(this.pais.getCodi()) ); 
       this.jPData.setRequestFocus();
    }
    @Override
    public void saveData() throws ExceptionHibernate  {                
        Pais temp = new Pais(pais.getId(),pais.getCodi(),pais.getAbreviatura(),pais.getNom()); 
        getData();        
        try{
            if (super.isInsert()){
                this.pais.setId(this.accio.add(this.pais));

            }else{
                this.accio.update(this.pais);                
            }   
            this.actualitzar();
        }catch (ExceptionHibernate ex){
            pais.setCodi(temp.getCodi());
            pais.setAbreviatura(temp.getAbreviatura());
            pais.setNom(temp.getNom());
            setData(); 
            throw new ExceptionHibernate (ex.getMessage()); 
            //JOptionPane.showMessageDialog(null,ex.getMessage() ,TDSLanguageUtils.getMessage("save.data.ERR"),JOptionPane.ERROR_MESSAGE);
        }finally{
            temp=null; 
        }
        
    }
    @Override
    public boolean validation (){
        if ("".equals(jPData.getCodeAuxiliar())){
            jPData.setErrorCode(TDSLanguageUtils.getMessage("jDialogPais.code.error"));
            return false;  
        }
        else if (Integer.parseInt(jPData.getCodeAuxiliar())<=0){
            jPData.setErrorCode(TDSLanguageUtils.getMessage("jDialogPais.code.positiu.error"));
            return false; 
        } 
        if ("".equals(jPData.getNameAuxiliar())){
            jPData.setErrorName(TDSLanguageUtils.getMessage("jDialogPais.name.error"));
            return false; 
        }
        return true; 
    }
    @Override
    public void removeData() throws ExceptionHibernate {
        this.accio.delete(this.pais);
        super.setObject(new Pais());
        this.actualitzar();
    }

    @Override
    public boolean isSaved() {        
        //if (!this.isValid()) return true; 
        if (this.isInsert()) return true; 
        if(this.pais==null) return true;
        if (!this.pais.getNom().equals(this.jPData.getNameAuxiliar())) return false;                
        if (!this.pais.getAbreviatura().equals(this.jPData.getCodeAuxiliar1())) return false ; 
        if(!this.pais.getCodi().equals(this.jPData.getCodeAuxiliar()) ) return false;                
        return true;
    }
    private void incrementarJTableXM(int e){
        super.incrementarPosRegistre(e);
    }
    public abstract void actualitzar(); 
}
